<?php

/**
 * @Author: wuchenge
 * @Date:   2022-04-26 14:51:22
 * @Email: ligui0506@126.com
 * @Descript:
 */
namespace App\Admin\Controllers;

use App\Http\Controllers\ApiController;
use App\Models\Article;
use App\Requests\ArticleRequest;
use Illuminate\Http\Request;

class ArticleController extends ApiController
{
    protected $model;
    protected $request;
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->model   = new Article();
    }

    public function add(ArticleRequest $request)
    {
        $title     = $request->input('title', '');
        $thumb     = $request->input('thumb', '');
        $content   = $request->input('content', '');
        $label_ids = $request->input('label_ids', []);
        $status    = $request->input('status', $this->model::STATUS_YES);

        $res = $this->model::add([
            'title'     => $title,
            'thumb'     => $thumb,
            'status'    => $status,
            'content'   => $content,
            'label_ids' => $label_ids,
        ]);

        if ($res['status']) {
            return $this->response();
        }

        return $this->failed($res['message']);
    }

    public function detail($id)
    {
        $res = $this->model::with(['labels'])->where('id', $id)->first();
        return $this->response($res);
    }

    public function modify(ArticleRequest $request)
    {
        $id        = $request->input('id', 0);
        $title     = $request->input('title', '');
        $thumb     = $request->input('thumb', '');
        $content   = $request->input('content', '');
        $label_ids = $request->input('label_ids', []);
        $status    = $request->input('status', $this->model::STATUS_YES);

        $model = $this->model::where('id', $id)->first();
        if (!$model) {
            return $this->failed();
        }
        // 更新
        $model->title   = $title;
        $model->thumb   = $thumb;
        $model->content = $content;
        $model->status  = $status ? $this->model::STATUS_YES : $this->model::STATUS_NO;
        $res            = $model->save();

        if ($res) {
            // 关联标签
            $model->labels()->sync($label_ids);
            return $this->response();
        }

        return $this->failed();
    }

    public function del(Request $request)
    {
        $id  = $request->input('id', 0);
        $res = $this->model::del($id);
        if ($res['status']) {
            return $this->response();
        }
        return $this->failed();
    }

    public function lists()
    {
        $title  = $this->request->input('title', '');
        $status = $this->request->input('status', 0);
        $page   = $this->request->input('page', 1);
        $limit  = $this->request->input('limit', 10);

        $with  = ['labels'];
        $order = ['created_at' => 'desc'];
        $field = ['*'];
        $where = [
            'nomal' => [],
        ];

        // 标题搜索
        if (!empty($title)) {
            $where['nomal'][] = ['title', 'like', '%' . $title . '%'];
        }

        // 显/隐搜索
        if (!empty($status)) {
            $where['nomal'][] = ['status', '=', $status];
        }

        $data = $this->model::lists(
            $field,
            $where,
            $page,
            $limit,
            $order,
            $with
        );

        return $this->response($data);
    }
}
